﻿// JScript 文件
//var MarkPoint;
//var strHtml;
//var machineId;


function createDriverDiv()
{
    
    var divContent = document.createElement("div");
    
    var labelArray = new Array("姓  名","电  话","驾驶证号","准驾机型");
    var textNameArray = new Array("txtDriverName","txtDriverPhone","txtDriverCode","txtDriverType");
    
    var labels = createTextNode(labelArray);
    var texts = createTexts(textNameArray);
    
    var tableTitles = new Array();
    for (var i = 0; i < 4; i++)
    {
        tableTitles[i] = document.createElement("span");
        with($(tableTitles[i]))
        {
            append("<br/>");
            append(labelArray[i]);
            append("<br/>");
        }            
    } 
    var tableValue = new Array(texts);        
    var table = new DataTable(tableTitles,tableValue, 1);
    
    $(divContent).empty();
    divContent.appendChild(table);    
    
    return divContent;    


}


function createMachineDiv()
{
    var divContent = document.createElement("div");
    
    var labelArray = new Array("牌照号","所有人姓名","所属单位","机车类型");
    var textNameArray = new Array("txtMachineCode","txtMachineOwner","txtMachineDept","txtMachineType");
    
    var labels = createTextNode(labelArray);
    var texts = createTexts(textNameArray);
    
    var tableTitles = new Array();
    for (var i = 0; i < 4; i++)
    {
        tableTitles[i] = document.createElement("span");
        with($(tableTitles[i]))
        {
            append("<br/>");
            append(labelArray[i]);
            append("<br/>");
        }            
    } 
    var tableValue = new Array(texts);        
    var table = new DataTable(tableTitles,tableValue, 1);
    
    $(divContent).empty();
    divContent.appendChild(table);    
    
    return divContent;    
}

function createToolDiv()
{
    var divContent = document.createElement("div");
    
    var labelArray = new Array("农具编码","种类名称","所有人姓名","所属单位");
    var textNameArray = new Array("txtToolCode","txtToolCatalog","txtToolOwner","txtToolDept");
    
    var labels = createTextNode(labelArray);
    var texts = createTexts(textNameArray);
    
    var tableTitles = new Array();
    for (var i = 0; i < 4; i++)
    {
        tableTitles[i] = document.createElement("span");
        with($(tableTitles[i]))
        {
            append("<br/>");
            append(labelArray[i]);
            append("<br/>");
        }            
    } 
    var tableValue = new Array(texts);        
    var table = new DataTable(tableTitles,tableValue, 1);
    
    $(divContent).empty();
    divContent.appendChild(table);    
    
    return divContent;    
}


function createJobDiv()
{
    var divContent = document.createElement("div");
    
    var labelArray = new Array("作业名称","牵引农具","折合系数","作业量");
    var textNameArray = new Array("txtJobName","txtJobTool","txtJobRatio","txtJobAmount");
    
    var labels = createTextNode(labelArray);
    var texts = createTexts(textNameArray);
    
    var tableTitles = new Array();
    for (var i = 0; i < 4; i++)
    {
        tableTitles[i] = document.createElement("span");
        with($(tableTitles[i]))
        {
            append("<br/>");
            append(labelArray[i]);
            append("<br/>");
            
        }            
    } 
    var tableValue = new Array(texts);        
    var table = new DataTable(tableTitles,tableValue, 1);
    
    $(divContent).empty();
    //$(divContent).height(300);
    divContent.appendChild(table);    
    
    return divContent;
}


//添加统计功能菜单
//yangxd  2011-7-27
//新增。
function createStatDiv() {
    var divContent = document.createElement("div");
    //$(divContent).attr("id", "statDiv");

//    var labelArray = new Array("按农机统计", "按地块统计", "按时间统计");
//    var textNameArray = new Array("txtJobName", "txtJobTool", "txtJobRatio", "txtJobAmount");

    var buttonLabellArray = new Array("当日作业统计", "季度作业统计");
    var buttonIdArray = new Array("btnStatDay", "btnStatQuarter"); //, "btnStatTime");
    var buttons = createButtons(buttonLabellArray, buttonIdArray);
    $(buttons[0]).bind("click", StatDay);
    $(buttons[1]).bind("click", StatQuarter); //replayGpsRoute


    $(divContent).empty();
    //$(divContent).height(300);
    for (var i = 0; i < 2; i++) {
        $(divContent).append(buttons[i]);
        //$(divContent).append("<br/>");
    }

    var divStat = document.createElement("div");
    $(divStat).attr("id", "statDiv");
    divContent.appendChild(divStat);

    return divContent;
}




function createControlDiv()
{
    var divContent = document.createElement("div");
    
    var btnLabelArray = new Array("农机位置","清理地图","地块信息","视频回放");//"农机轨迹","遥感长势",
    var BtnNameArray = new Array("btnMachineLocation","btnClearRoute","btnFieldLayer","btnReplayVideo");//"btnMachineRoute","btnRsGrowing",
    
    var buttons = createButtons(btnLabelArray,BtnNameArray);
    $(buttons[0]).bind("click",startMonitorGps);
    $(buttons[1]).bind("click",clearAllRoutes);//replayGpsRoute
    $(buttons[2]).bind("click",AddFieldLayer);
    $(buttons[3]).bind("click",replayVideo);
    //$(buttons[4]).bind("click",AddRsGrowing);
    //$(buttons[5]).bind("click",);

    
    var tableTitles = new Array();
    var tableValue = new Array();
    for (var i = 0; i < 2; i++)
    {
        tableTitles[i] = document.createElement("span");
        tableValue[i] = document.createElement("span");
        with($(tableTitles[i]))
        {
//            append("<br/>");
            $(buttons[i]).width(100);
            append(buttons[i]);
            append("<br/>");
            
        }
        with($(tableValue[i]))   
        {
            $(buttons[i+2]).width(120);
            append(buttons[i+2]);
            append("<br/>");
        }         
    } 
    var tableValue = new Array(tableValue);        
    var table = new DataTable(tableTitles,tableValue, 1);
    
    $(divContent).empty();
    divContent.appendChild(table);    
    
    return divContent;    

}


function createGPSDiv()
{
    var divContent = document.createElement("div");
    
//    var btnLabelArray = new Array("10分钟轨迹","30分钟轨迹","1小时轨迹","2小时轨迹","4小时轨迹","8小时轨迹","12小时轨迹","24小时轨迹");
//    var BtnNameArray = new Array("btn10mRoute","btn30mRoute","btn1hRoute","btn2hRoute","btn4hRoute","btn8hRoute","btn12hRoute","btn24hRoute");
//    
//    var buttons = createButtons(btnLabelArray,BtnNameArray);
    
    var btnLabelArray = new Array("轨迹查询","实时轨迹");//,"停止轨迹更新"
    var BtnNameArray = new Array("btnRouteSearch","btnRealTimeRoute");//,"btnStopRefresh"
    
    var buttons = createButtons(btnLabelArray,BtnNameArray);
    
    var labelNames = new Array("开始日期","小时","分钟","结束日期","小时","分钟");
    var txtDateNames = new Array("fromDate","fromHour","fromMinute","toDate","toHour","toMinute");
    var labels = createTextNode(labelNames);
    var txtDate = createTexts(txtDateNames);
    
    for(var i=0;i < 6;i++)
    {
        $(txtDate[i]).width(72);
    }
    
    $(txtDate[0]).width(100);
    $(txtDate[1]).width(48);
    $(txtDate[2]).width(48);
    $(txtDate[3]).width(100);
    $(txtDate[4]).width(48);
    $(txtDate[5]).width(48);
    
    
    $(txtDate[1]).val(08);
    $(txtDate[2]).val(00);
    $(txtDate[4]).val(18);
    $(txtDate[5]).val(00);
    
    var tableTitles = new Array();
    var tableValue = new Array();
    for (var i = 0; i < 3; i++)
    {
        tableTitles[i] = labels[i];
    } 
    for (var i = 0; i < 3; i++)
    {
        tableValue[i] = new Array();
    }
    for(var j = 0;j < 3;j++)
    {
        tableValue[0][j] = txtDate[j];
        tableValue[1][j] = labels[j + 3];
        tableValue[2][j] = txtDate[j + 3];
    }
    
    var table = new DataTable(tableTitles,tableValue, 0);
    
    $(buttons[0]).bind("click",searchGpsRoute);
    $(buttons[1]).bind("click",btnRealGPS_onclick);
    
    
    $(divContent).empty();
    $(divContent).append(table);
    $(divContent).append(buttons);

    return divContent;    
}


//real time gps monitoring
function btnRealGPS_onclick()
{
    var id;
    if($("#btnRealTimeRoute").val() == "实时轨迹")
    {
        id = window.setInterval(refreshGpsRoute,10000); 
        $("#btnRealTimeRoute").val("停止刷新");
    }
    else
    {
        window.clearInterval(id);
        $("#btnRealTimeRoute").val("实时轨迹");
    }
    
}


function refreshGpsRoute()
{
    var machineId = $("#txtMachineCode").val();
    var now = new Date();
    
    var startDate = now.toLocaleDateString();// now.getYear() + "-" + now.getMonth() + "-" + now.getDay();//$("#fromDate").val();
    
    
    var startHour = "08";// $("#fromHour").val();
    var startMinute = "00";//$("#fromMinute").val();
    var endDate = startDate;//$("#toDate").val();
    var endHour = now.getHours();// $("#toHour").val();
    var endMinute = now.getMinutes();//$("#toMinute").val();

    var params = new Array(machineId,startDate,startHour,startMinute,endDate,endHour,endMinute);
    
    if(machineId.length == 0)
    {
        alert("请先通过鼠标在地图上选择需要查看的农机！");
        return;
    }
    
    
    var strUrl = "WebServices/getGpsRoute.aspx";
    $.getJSON(strUrl,
        {   MachineId:machineId,
            StartDate:startDate,
            StartHour:startHour,
            StartMinute:startMinute,
            EndDate:endDate,
            EndHour:endHour,
            EndMinute:endMinute,
            Tmp:Math.random()
        },function(json)
        {
            var nPoints = json.Points.length;
            var Points = new Array();
            for (var i=0;i<nPoints;i++)
            {
                Points[i] = new GLatLng(json.Points[i].lat,json.Points[i].lon);
            }
            var machineId = json.Points[0].MachineId;
            //gmap.clearOverlays();
            createPolyline(Points,gmap);
            var pointMac = tranGLatLngToDMS(Points[nPoints-1]);
//            var strHtml = "<table><tr><td>";
//            strHtml += "农机代码</td><td>" + machineId + "</td></tr><tr><td>";
//            strHtml += "东经</td><td>" + pointMac[0] + "</td></tr><tr><td>";
//            strHtml += "北纬</td><td>" + pointMac[1] + "</td></tr><tr><td>";
//            strHtml += "</table>";

            var strHtml = "<table><tr><td>";
            strHtml += "农机代码</td><td>" + json.Machines[i].deviceCode + "</td></tr><tr><td>";
            //strHtml += "作业类型</td><td>" + json.Machines[i].jobName + "</td></tr><tr><td>";
            strHtml += "东经</td><td>" + machineLocation[0] + "</td></tr><tr><td>";
            strHtml += "北纬</td><td>" + machineLocation[1] + "</td></tr><tr><td>";
            strHtml += "<input type='button' value='详细信息' onclick='detailsClick()' /></td></tr>";
            strHtml += "</table>";               
            
            
            
            createMarker(Points[nPoints-1], gmap, strHtml,machineId);
        });        
    


}




//查询GPS详细轨迹信息
//前置条件：设备编号、开始日期时间、结束日期时间
//后置输出：在地图上叠加GPS轨迹线
function searchGpsRoute() {
    var testId = 0;

    var machineId = $("#txtMachineCode").val();
    var startDate = $("#fromDate").val();
    var startHour = $("#fromHour").val();
    var startMinute = $("#fromMinute").val();
    var endDate = $("#toDate").val();
    var endHour = $("#toHour").val();
    var endMinute = $("#toMinute").val();

    var params = new Array(machineId, startDate, startHour, startMinute, endDate, endHour, endMinute);

    //    if(event.data.type == "scope")
    //    {

    for (var i = 0; i < 7; i++) {
        if (params[i].length == 0) {
            if (i == 0)
                alert("请先通过鼠标在地图上选择需要查看的农机！");
            else
                alert("请填写完整时间参数！");

            return;
        }
    }
    //检查时间参数的有效性

    var strUrl = "WebServices/getGpsRoute.aspx";
    $.getJSON(strUrl,
            { MachineId: machineId,
                StartDate: startDate,
                StartHour: startHour,
                StartMinute: startMinute,
                EndDate: endDate,
                EndHour: endHour,
                EndMinute: endMinute,
                Tmp: Math.random()
            }, function(json) {
                var nPoints = json.Points.length;
                var Points = new Array();
                for (var i = 0; i < nPoints; i++) {
                    Points[i] = new GLatLng(json.Points[i].lat, json.Points[i].lon);
                }
                machineId = json.Points[0].MachineId;
                createPolyline(Points, gmap);
                
//                var pointMac = tranGLatLngToDMS(Points[nPoints - 1]);
//                strHtml = "<table><tr><td>";
//                strHtml += "农机代码</td><td>" + machineId + "</td></tr><tr><td>";
//                strHtml += "东经</td><td>" + pointMac[0] + "</td></tr><tr><td>";
//                strHtml += "北纬</td><td>" + pointMac[1] + "</td></tr><tr><td>";
//                strHtml += "</table>";

//                createMarker(Points[0], gmap, strHtml, machineId);
                
                
                doTimer(Points);


                //for (var j = 0; j < (nPoints - 1); j++) {
                //MarkPoint = Points[j];

            });

}

//function delayCreateMarker() {
//    createMarker(MarkPoint, gmap, strHtml, machineId);
//    var t = setTimeout(delayCreateMarker, 1000);


//}

//var c = 0;
//var t;
//var timer_is_on = 0;

//function timedCount() {
//    document.getElementById('txt').value = c;
//    c = c + 1;
//    t = setTimeout("timedCount()", 1000);
//}

//function doTimer() {
//    if (!timer_is_on) {
//        timer_is_on = 1;
//        timedCount();
//    }
//}

function onButtonClick(event)
{
    var buttonId = event.data.buttonId;
}

//启动GPS监控，在地图上对农机进行定位
//前置条件：无
//后置输出：显示农场全部农机作业最近状态的位置，并在地图上以图标形式展现
function startMonitorGps()
{
    var strUrl = "WebServices/getGpsLocations.aspx";
    $.getJSON(strUrl,{TMP:Math.random()},function(json)
        {
            //alert(json.Machines.length);
            var nMachines = json.Machines.length;
            
            //gmap.clearOverlays();
            for(var i = 0; i < nMachines; i++ )
            {
                //var pointLocation =new GLatLng(parseFloat(json.Machines[i].lat),parseFloat(json.Machines[i].lon));
                var pointLocation =new GLatLng(json.Machines[i].lat,json.Machines[i].lon);
                var machineLocation = tranGLatLngToDMS(pointLocation);
                
                var strHtml = "<table><tr><td>";
                strHtml += "农机代码</td><td>" + json.Machines[i].deviceCode + "</td></tr><tr><td>";
                //strHtml += "作业类型</td><td>" + json.Machines[i].jobName + "</td></tr><tr><td>";
                strHtml += "东经</td><td>" + machineLocation[0] + "</td></tr><tr><td>";
                strHtml += "北纬</td><td>" + machineLocation[1] + "</td></tr><tr><td>";
                strHtml += "<input type='button' value='详细信息' onclick='detailsClick()' /></td></tr>";
                strHtml += "</table>";        
                //createMarker(pointLocation, gmap, strHtml);
                createMarker2(pointLocation, gmap, strHtml,json.Machines[i].deviceCode);
            }
        });    

}

//启动Gps轨迹回放
//前置条件：用户必须先在地图上选中某一台农机
//后置输出：显示当前选中农机的当天全部工作轨迹
function replayGpsRoute()
{
    openLegend();
    
}

function clearAllRoutes()
{
    gmap.clearOverlays();
    MAP_TYPE = "GMAP";
}

function replayVideo()
{
    window.open("http://whf1.91ddns.com ");
}

function AddFieldLayer()
{
    MAP_TYPE = "IMSMAP";
    var divWidth = $("#map").width() * 2; //gmap.getSize().width - 6; 
    var divHeight = $("#map").height() * 2;//gmap.getSize().height - 6; 
          
    var mapscope= comm.fun.method_2();
    
    showWaitImage();//请求地图
    var requestBaseUrl = "WebServices/getVectorMap.aspx";
    $.get(requestBaseUrl,{  SERVICENAME:"hx", MAP_TYPE:"IMSMAP", MAPBOX:mapscope[0], MAPWIDTH:divWidth, MAPHEIGHT:divHeight,TMP:Math.random() },
                            function(data){ 
                                var xmlDocument = data.documentElement;
                                var nodes = xmlDocument.getElementsByTagName("URL");
                                var strResultUrl = "";
                                if(nodes[0].firstChild != null)
                                    strResultUrl = nodes[0].firstChild.nodeValue;
                                else
                                    strResultUrl = "Picture/mapnotexist.gif";
                                if(strResultUrl=="no url") {alert("没有默认分级刻度!请重新选择或手动设置!");return;}
                                if(strResultUrl.length == 0)
                                    strResultUrl = "Picture/mapnotexist.gif";
                                
                                    gmap.clearOverlays();
                                    if (MAP_TYPE == "IMSMAPONLY")
                                        gmap.addOverlay(new ImsMapOverlay(mapscope[1],"","#dcdcdc",0,"#dcdcdc"));
                                    imsOverlay = new ImsMapOverlay(mapscope[1],strResultUrl,"transparent",0,"#dcdcdc");
                                    gmap.addOverlay(imsOverlay);                                                            
                                    hideWaitImage(); 
                            });                                                          
}




function detailsClick()
{
    var machineId = $("#txtMachineCode").val();
    
    
    var strUrl="WebServices/getMachineDetails.aspx";
    $.getJSON(strUrl, { buttonId: machineId, tmp: Math.random() }, function(json)
        {
            $("#txtDriverName").val(json.Details[0].DriverName);
            $("#txtDriverPhone").val(json.Details[0].DriverPhone);
            $("#txtDriverCode").val(json.Details[0].DriverCode);
            $("#txtDriverType").val(json.Details[0].DriverType);
            $("#txtMachineCode").val(json.Details[0].MachineCode);
            $("#txtMachineOwner").val(json.Details[0].MachineOwner);
            $("#txtMachineDept").val(json.Details[0].MachineDept);
            $("#txtMachineType").val(json.Details[0].MachineType);
            $("#txtToolCode").val(json.Details[0].ToolCode);
            $("#txtToolCatalog").val(json.Details[0].ToolCatalog);
            $("#txtToolOwner").val(json.Details[0].ToolOwner);
            $("#txtToolDept").val(json.Details[0].ToolDept);
            $("#txtJobName").val(json.Details[0].JobName);
            $("#txtJobTool").val(json.Details[0].JobTool);
            $("#txtJobRatio").val(json.Details[0].JobRatio);
            $("#txtJobAmount").val(json.Details[0].JobAmount);
        
        });
}


function StatDay() {
    var machineId = $("#txtMachineCode").val();
    var strUrl = "WebServices/getStatInfo.aspx";

    $.getJSON(strUrl, { buttonId: machineId, tmp: Math.random() }, function(json) {
        var nWorks = json.Details.length;
        var WorkNames = new Array();
        var WorkAmounts = new Array();
        var WorkMoneys = new Array();
        var tableValue = new Array();
        for (var i = 0; i < nWorks; i++) {
            WorkNames[i] = json.Details[i].WorkName;
            WorkAmounts[i] = json.Details[i].WorkAmount;
            WorkMoneys[i] = json.Details[i].WorkMoney;
            tableValue[i] = createTextNode(new Array(WorkNames[i], WorkAmounts[i], WorkMoneys[i]));
        }

        var labelArray = new Array("作业名称", "作业量", "作业金额");
        var tableTitles = createTextNode(labelArray);


        var statTable = new DataTable(tableTitles, tableValue, 0);
        $("#statDiv").empty();
        $("#statDiv").addClass("resultTable")
        $("#statDiv").append(statTable);

    });

}

function StatQuarter() {
    var machineId = $("#txtMachineCode").val();
    var strUrl = "WebServices/getStatQuarter.aspx";

    $.getJSON(strUrl, { buttonId: machineId, tmp: Math.random() }, function(json) {
        var nWorks = json.Details.length;
        var WorkNames = new Array();
        var WorkAmounts = new Array();
        var WorkMoneys = new Array();
        var tableValue = new Array();
        for (var i = 0; i < nWorks; i++) {
            WorkNames[i] = json.Details[i].WorkName;
            WorkAmounts[i] = json.Details[i].WorkAmount;
            WorkMoneys[i] = json.Details[i].WorkMoney;
            tableValue[i] = createTextNode(new Array(WorkNames[i], WorkAmounts[i], WorkMoneys[i]));
        }

        var labelArray = new Array("作业名称", "作业量", "作业金额");
        var tableTitles = createTextNode(labelArray);


        var statTable = new DataTable(tableTitles, tableValue, 0);
        $("#statDiv").empty();
        $("#statDiv").addClass("resultTable")
        $("#statDiv").append(statTable);

    });


}


//function DateBase(){ 
//var now = new Date();//当前日期 
//var year = now.getYear();//年份后两位 
//var fullYear = now.getFullYear();//获取完整的年份(4位,1970-????) 
//now.getMonth();      //获取当前月份(0-11,0代表1月) 
//now.getDate();       //获取当前日(1-31) 
//now.getDay();        //获取当前星期X(0-6,0代表星期天) 
//now.getTime();       //获取当前时间(从1970.1.1开始的毫秒数) 
//now.getHours();      //获取当前小时数(0-23) 
//now.getMinutes();    //获取当前分钟数(0-59) 
//now.getSeconds();    //获取当前秒数(0-59) 
//now.getMilliseconds();   //获取当前毫秒数(0-999) 
//now.toLocaleDateString();    //获取当前日期 
//var mytime=now.toLocaleTimeString();    //获取当前时间 
//now.toLocaleString();       //获取日期与时间 

//}